package com.aiti.lulian.workflow;


import com.aiti.lulian.mapper.TaskInstanceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;


@Service
public class TaskManager {

    @Autowired
    private TaskInstanceMapper taskInstanceMapper;

    @Autowired
    private ProcessInstanceManager processInstanceManager;

    @Autowired
    private TaskHistoryService taskHistoryService;

    public TaskInstance save(TaskInstance taskInstance) {
        taskInstanceMapper.insert(taskInstance);
        return taskInstance;
    }

    public TaskInstance update(TaskInstance taskInstance) {
        taskInstanceMapper.updateById(taskInstance);
        return taskInstance;
    }

    public TaskInstance findById(String id) {
        TaskInstance taskInstance = taskInstanceMapper.selectById(id);
        if (taskInstance != null) {
            // 加载流程实例
            ProcessInstance instance = processInstanceManager.findById(taskInstance.getProcessInstanceId());
            if(instance != null){
                taskInstance.setProcessInstance(instance);
            }

        }
        return taskInstance;
    }

    public List<TaskInstance> findByProcessInstanceId(String processInstanceId) {
        return taskInstanceMapper.findByProcessInstanceId(processInstanceId);
    }

    public List<TaskInstance> findByTaskDefinitionId(String taskDefinitionId) {
        return taskInstanceMapper.findByTaskDefinitionId(taskDefinitionId);
    }

    public List<TaskInstance> findByTaskDefinitionIdAndInstanceId(String taskDefinitionId, String instanceId){
        return taskInstanceMapper.findByTaskDefinitionIdAndInstanceId(taskDefinitionId, instanceId);
    }

    public List<TaskInstance> findByProcessInstanceIdAndStatus(String processInstanceId, TaskStatus status) {
        return taskInstanceMapper.findByProcessInstanceIdAndStatus(processInstanceId, status);
    }

    public List<TaskInstance> findByAssigneeIdAndStatus(String assigneeId, TaskStatus status) {
        return taskInstanceMapper.findByAssigneeIdAndStatus(assigneeId, status);
    }

    public List<TaskInstance> findPendingTasksByAssignee(String assigneeId) {
        return taskInstanceMapper.findPendingTasksByAssignee(assigneeId);
    }

    public List<TaskInstance> findPendingTasksByAssignees(List<String> assigneeIds) {
        return taskInstanceMapper.findPendingTasksByAssignees(assigneeIds);
    }

    @Transactional
    public TaskInstance createTask(ProcessInstance processInstance, Node node,
                                   String assigneeId, Map<String, Object> variables) {
        TaskInstance task = new TaskInstance();
        task.setId("task_" + System.currentTimeMillis());
        task.setProcessInstanceId(processInstance.getId());
        task.setProcessInstance(processInstance);
        task.setTaskDefinitionId(node.getId());
        task.setName(node.getName());

        // 设置任务类型和分配信息
        task.setType(TaskType.NORMAL);
        task.setAssigneeType(AssigneeType.valueOf(node.getAssigneeType()));
        task.setAssigneeId(assigneeId);

        // 设置变量
        if (variables != null) {
            task.setVariables(variables);
        }

        // 保存任务
        taskInstanceMapper.insert(task);

        // 记录任务创建历史
        taskHistoryService.recordTaskHistory(
                processInstance.getId(),
                node.getId(),
                task.getId(),
                ActionType.CREATE,
                "system",
                null,
                variables
        );

        return task;
    }

    @Transactional
    public void completeTask(String taskId, Map<String, Object> variables, String comment, String actor) {
        TaskInstance taskInstance = findById(taskId);
        if (taskInstance != null) {
            taskInstance.complete(variables, comment);
            taskInstanceMapper.updateById(taskInstance);

            // 记录任务完成历史
            taskHistoryService.recordTaskHistory(
                    taskInstance.getProcessInstanceId(),
                    taskInstance.getTaskDefinitionId(),
                    taskInstance.getId(),
                    ActionType.COMPLETE,
                    actor,
                    comment,
                    variables
            );
        }
    }

    @Transactional
    public void cancelTask(String taskId, String comment, String actor) {
        TaskInstance taskInstance = findById(taskId);
        if (taskInstance != null) {
            taskInstance.cancel();
            taskInstanceMapper.updateById(taskInstance);

            // 记录任务取消历史
            taskHistoryService.recordTaskHistory(
                    taskInstance.getProcessInstanceId(),
                    taskInstance.getTaskDefinitionId(),
                    taskInstance.getId(),
                    ActionType.CANCEL,
                    actor,
                    comment,
                    null
            );
        }
    }
}